package com.actions.bluetoothbox2.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.app.BrowserActivity;
import com.actions.bluetoothbox2.util.DButils;
import com.actions.bluetoothbox2.util.SchoolReportAdapter;
import com.actions.bluetoothbox2.util.SchoolReportInfo;
import com.actions.ibluz.manager.BluzManager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SchoolReportFragment extends Fragment {

    private static final String LOGTAG = "SchoolReportFragment";
    private BrowserActivity mActivity;
    private BluzManager mBluzManager;
    private RatingBar ratingbar;
    private TextView tv_his;
    private Button btn_star;
    private ListView listview;
    private List<SchoolReportInfo> list;
    private String data;//当前时间
    private SchoolReportAdapter adapter;

    public SchoolReportFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_school_report, container, false);

        mActivity = (BrowserActivity) getActivity();
        mBluzManager = mActivity.getBluzManager();
        mActivity.tv_title.setText(getResources().getText(R.string.school_report));
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.title_back.setVisibility(View.VISIBLE);
        mActivity.mGlobalInfoLayout.setVisibility(View.GONE);

        ratingbar = (RatingBar) view.findViewById(R.id.ratingbar);
        btn_star = (Button) view.findViewById(R.id.btn_star);
        tv_his = (TextView) view.findViewById(R.id.tv_his);
        listview = (ListView) view.findViewById(R.id.listview);
        list = new ArrayList<>();
        adapter = new SchoolReportAdapter(getActivity(), list);
        list.addAll(DButils.queryReportAll());

        if (null == list || list.size() == 0) {
            tv_his.setVisibility(View.VISIBLE);
        } else {
            tv_his.setVisibility(View.GONE);
//                Collections.sort(list);
            Collections.reverse(list);//对histories 集合中的数据进行倒叙排序
            listview.setAdapter(adapter);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
//获取当前时间
        Date date = new Date(System.currentTimeMillis());
        data = simpleDateFormat.format(date);

        if (list.get(0).getTime().equals(data)) {
            ratingbar.setRating(list.get(0).star);
        }

        btn_star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SchoolReportInfo info = new SchoolReportInfo();
                int star = ratingbar.getNumStars();
                info.setStar(star);
                info.setTime(data);

                if (list.get(0).getTime().equals(data)) {
                    info.setId(list.get(0).getId());
                    DButils.updateReport(info);
                } else {
                    DButils.insertReport(info);
                    list.add(info);
                }
            }
        });

        return view;
    }


}
